import commonStore from "@/store/commonStore";

const useRedirect = () => {

  const setLastPage = () => {
    const useCommonStore = commonStore()
    const pages = getCurrentPages()
    const lastPage = pages[pages.length - 1].$page.fullPath || 'pages/index/index'
    console.log('lastPage: ', lastPage);
    useCommonStore.updateValueByKey('lastPath', lastPage)
  }

  const toRedirect = (path) => {
    const useCommonStore = commonStore()
    if (!path) {
      path = useCommonStore.lastPath
    }
    uni.redirectTo({
        url: path
      })
  }

  return {
    toRedirect,
    setLastPage,
  }
}

export default useRedirect
